#include "thread_pool.h"

void logMessage(const char *message) {
    FILE *log_file = fopen("server_log.txt", "a");
    if(log_file == NULL) {
        perror("Unable to open log file");
        return;
    }
    time_t now = time(NULL);
    struct tm *t = localtime(&now);
    fprintf(log_file, "%04d-%02d-%02d %02d:%02d:%02d - %s\n",
            t->tm_year + 1900, t->tm_mon + 1, t->tm_mday,
            t->tm_hour, t->tm_min, t->tm_sec, message);
    fclose(log_file);
}


void logCLientConnection(int client_fd) {
    struct sockaddr_in client_addr;
    socklen_t len = sizeof(client_addr);
    if(getpeername(client_fd, (struct sockaddr *)&client_addr, &len) == 0) {
        char client_info[256];
        memset(client_info, 0, sizeof(client_info));
        snprintf(client_info, sizeof(client_info), "Client connected: %s:%d",
                    inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
    logMessage(client_info);
    }
}

void logClientRequest(int client_fd, const char *request) {
    char log_info[128];
    snprintf(log_info, sizeof(log_info), "client %d requeset: %s", 
                client_fd, request);
    logMessage(log_info);
}